﻿//======================================================================
//  序列化实现者
//====================================================================== 

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Common
{
    /// <summary>
    /// 序列化实现者
    /// </summary>
    public sealed class Serializer
    {
        /// <summary>
        /// 将object对象序列化成XML
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ObjectToXML(object o)
        {
            XmlSerializer ser = new XmlSerializer(o.GetType());
            MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
            ser.Serialize(writer, o);
            writer.Close();
            return Encoding.UTF8.GetString(mem.ToArray());
        }
        /// <summary>
        /// 将XML反序列化成对象
        /// </summary>
        /// <param name="s"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static object XMLToObject(string s, Type t)
        {
            XmlSerializer mySerializer = new XmlSerializer(t);
            StreamReader mem2 = new StreamReader(new MemoryStream(System.Text.Encoding.Default.GetBytes(s)), System.Text.Encoding.UTF8);
            return mySerializer.Deserialize(mem2);
        }


        /// <summary>
        /// 将object对象序列化成XML
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ObjectToXML<T>(T t)
        {
            return ObjectToXML<T>( t, Encoding.UTF8 );
        }

        /// <summary>
        /// 将object对象序列化成XML
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ObjectToXML<T>( T t, Encoding encoding )
        {
            XmlSerializer ser = new XmlSerializer( t.GetType() );
            using ( MemoryStream mem = new MemoryStream() )
            {
                using ( XmlTextWriter writer = new XmlTextWriter( mem, encoding ) )
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add( "", "" );
                    ser.Serialize( writer, t, ns );
                    return encoding.GetString( mem.ToArray() ).Trim();
                }
            }
        }

        /// <summary>
        /// 将XML反序列化成对象
        /// </summary>
        /// <param name="s"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T XMLToObject<T>(string source)
        {
            return XMLToObject<T>( source, Encoding.UTF8 );
        }

        /// <summary>
        /// 将XML反序列化成对象
        /// </summary>
        /// <param name="s"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static T XMLToObject<T>( string source, Encoding encoding )
        {
            XmlSerializer mySerializer = new XmlSerializer( typeof( T ) );
            using ( MemoryStream stream = new MemoryStream( encoding.GetBytes( source ) ) )
            {
                return ( T )mySerializer.Deserialize( stream );
            }
        }

        /// <summary>
        /// 二进制方式序列化对象
        /// </summary>
        /// <param name="testUser"></param>
        public static string Serialize<T>(T obj)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);

            return ms.ToString();
        }

        /// <summary>
        /// 二进制方式反序列化对象
        /// </summary>
        /// <returns></returns>
        public static T DeSerialize<T>(string str) where T:class
        {
            MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str));
            BinaryFormatter formatter = new BinaryFormatter();
            T t = formatter.Deserialize(ms) as T;
            return t;
        }
    }
}
